package com.echoesnet.echoesshortvideo.Activities;

import android.app.Activity;
import android.arch.lifecycle.Observer;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.echoesnet.echoesshortvideo.R;
import com.echoesnet.echoesshortvideo.Utils.CommonUtils;
import com.echoesnet.echoesshortvideo.Utils.ToastUtils;
import com.echoesnet.echoesshortvideo.ViewModels.StepRegisterViewModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_SINGLE_TOP;

public class BindPasswordAct extends BaseActivity
{


    @BindView(R.id.cancle)
    ImageView cancle;
    @BindView(R.id.phone)
    TextView phone;
    @BindView(R.id.password)
    EditText edPassword;
    @BindView(R.id.nextOk)
    ImageView nextOk;
    @BindView(R.id.scrollLayout)
    RelativeLayout rl;
    private String code;
    private StepRegisterViewModel lModel;
    //屏幕高度
    private int screenHeight = 0;
    //软件盘弹起后所占高度阀值
    private int keyHeight = 0;

    @BindView(R.id.root_layout)
    View activityRootView;
    private Activity mAct;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_bind_password);
        mAct=this;
        ButterKnife.bind(this);
        phone.setText("+86 "+getIntent().getStringExtra("phone"));
        code=getIntent().getStringExtra("code");
        edPassword.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {

                if(!CommonUtils.verifyInput(1, edPassword.getText().toString().trim()))
                {
                    nextOk.setEnabled(false);
                    nextOk.setAlpha(0.5f);
                }
                else
                {
                    nextOk.setEnabled(true);
                    nextOk.setAlpha(1f);
                }
            }
            @Override
            public void afterTextChanged(Editable s)
            {

            }
        });

        lModel=new StepRegisterViewModel();
        lModel.getLiveData().observe(this, new Observer<String>()
        {
            @Override
            public void onChanged(@Nullable String type)
            {
                if (type==null)
                    return;

                if ("bindPhoneOk".equals(type))
                {
                    Toast.makeText(BindPasswordAct.this, "绑定成功", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(mAct,MySettingActivity.class);
                    intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(intent);
                    finish();
                }


            }
        });

        nextOk.setEnabled(false);

    }


    @OnClick({R.id.cancle, R.id.layout_hint, R.id.password, R.id.nextOk})
    public void onViewClicked(View view)
    {
        switch (view.getId())
        {
            case R.id.cancle:
                finish();
                break;
            case R.id.phone_code:
                break;
            case R.id.nextOk:
                lModel.bindPhone(phone.getText().toString().replace("+86","").trim()
                        ,edPassword.getText().toString().trim());
                break;
        }

        //获取屏幕高度
        screenHeight = CommonUtils.getScreenHeight(this);
        //阀值设置为屏幕高度的1/3
        keyHeight = screenHeight/3;
        //添加layout大小发生改变监听器
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
    }

    private void updateViewHeight(boolean flag)
    {
        RelativeLayout.LayoutParams pa= (RelativeLayout.LayoutParams) rl.getLayoutParams();
        pa.topMargin=flag?LoginAct.TOP_MARGIN:0;
        rl.setLayoutParams(pa);
    }

    ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
    {

        @Override
        public void onGlobalLayout()
        {
            activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(layoutListener);

            Rect r = new Rect();
            activityRootView.getWindowVisibleDisplayFrame(r);

            //现在认为只要控件将Activity向上推的高度超过了1/3屏幕高，就认为软键盘弹起
            if (screenHeight-r.bottom  > keyHeight)
            {
                updateViewHeight(true);
            } else
            {
                updateViewHeight(false);
            }
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
    };
}
